package com.example.com.daervideo.utils;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.util.Date;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;


@SuppressWarnings("restriction")
public class DESUtils {

    // 密钥
    private final static String SECRETKEY = "1bd4ac3849917342b1fb247c22a59d03";
    // 向量
    private final static String IV = "56910372";
    // 加解密统一使用的编码方式
    private final static String ENCODING = "utf-8";


    // 统一加密方法
    public static String utilMD5Encode(String plainText, String keyString) {
        try {
            String time = keyString;
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 将盐数据传入消息摘要对象
            md.update(time.getBytes());
            // 将口令的数据传给消息摘要对象
            md.update(plainText.getBytes("UTF-8"));
            return Base64.encodeToString(md.digest(), Base64.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 统一加密方法
    public static String utilEncode(String plainText) {
        String desEncodeString = encode(plainText);
        String urlStr = null;
        try {
            byte[] encode = Base64.encode(desEncodeString.getBytes("utf-8"), Base64.DEFAULT);
            urlStr = new String(encode, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return urlStr;
    }


    // 统一解密方法
    public static String utilDecode(String plainText) {
        try {
            String urlDeString = java.net.URLDecoder.decode(plainText, "utf-8");
            byte[] decodeBt = Base64.decode(urlDeString, Base64.DEFAULT);
            String decodeString = decode(new String(decodeBt));
            return decodeString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 3DES加密
     *
     * @param plainText 加密文本
     * @return
     * @throws Exception
     */
    public static String encode(String plainText) {
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(SECRETKEY.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
            deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
            byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING));
            String base64String =new String(Base64.encode(encryptData, Base64.DEFAULT),"utf-8") ;
            return base64String;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 3DES解密
     *
     * @param encryptText 加密文本
     * @return
     * @throws Exception
     */
    public static String decode(String encryptText) {
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(SECRETKEY.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DeSede");
            deskey = keyfactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance("DeSede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
            byte[] decryptData = cipher.doFinal(Base64.decode(encryptText, Base64.DEFAULT));
            return new String(decryptData, ENCODING);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
